package com.edgclub.edg.activity;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.Constant;
import com.edgclub.edg.R;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.RechargeApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.RechargeParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.uc.DeleteDialog;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jungly.gridpasswordview.GridPasswordView;
import com.rockerhieu.emojicon.emoji.SerializeManager;

/**
 * 修改支付密码
 *
 *
 */
public class GzPayPassUpdateActivity extends GzBaseTooolBarActivity implements
		View.OnClickListener,VolleyListener {

	private Activity activity;

	private GridPasswordView gridPasswordView;
	private TextView pass_tag;

	private boolean isLoading = false; // 是否正在加载
	private UserPrefUtils userPrefUtils;// 用户信息存储
	private RechargeApi rechargeApi;
	private boolean isFirst = true;

	private String modifyCode="";
	private String password="";
	private String frompage="";
	private String oldpass="";

	@Override
	public int getContentView() {
		activity = this;
		return R.layout.activity_gz_apply_pass_update;
	}

	@Override
	public void setTitleBarView() {
		setCustomActionBar();
		title.setText("修改支付密码");
		main_back.setVisibility(View.VISIBLE);
		main_back.setOnClickListener(this);


	}
	@Override
	public void initView() {
		frompage=getIntent().getStringExtra("frompage");
		modifyCode=getIntent().getStringExtra("modifyCode");
		pass_tag = (TextView) findViewById(R.id.pass_tag);
		gridPasswordView = (GridPasswordView) findViewById(R.id.pswView);
		gridPasswordView.setOnPasswordChangedListener(onPasswordChangedListener);
		if("setpass".equals(frompage)||"forget".equals(frompage)){
			title.setText("设置支付密码");
			pass_tag.setText("请输入新的支付密码");
		}
	}
	@Override
	public void initData() {
		userPrefUtils = new UserPrefUtils(this);
		rechargeApi = new RechargeApi(this);

	}
	GridPasswordView.OnPasswordChangedListener onPasswordChangedListener=new GridPasswordView.OnPasswordChangedListener() {
		@Override
		public void onTextChanged(String psw) {

		}

		@Override
		public void onInputFinish(String psw) {

			if("请输入新的支付密码".equals(pass_tag.getText())||"两次输入不一致,请重新输入".equals(pass_tag.getText())){
				password=psw;
				pass_tag.setTextColor(Color.parseColor("#f02020"));
				gridPasswordView.clearPassword();
				pass_tag.setText("请再次确认密码");
			}else if("请再次确认密码".equals(pass_tag.getText())){
				if(password.equals(psw)&&"setpass".equals(frompage)||password.equals(psw)&&"forget".equals(frompage)){
					setPassword();
				}else if(password.equals(psw)&&"update".equals(frompage)){
					updatePassword();
				}else{
					pass_tag.setText("两次输入不一致,请重新输入");
					pass_tag.setTextColor(Color.parseColor("#f02020"));
					password="";
					gridPasswordView.clearPassword();
				}
			}else if("请输入支付密码，验证身份".equals(pass_tag.getText())){
					oldpass=psw;
					checkPassword();
			}

		}
	};

	private void setPassword() {
		if (isLoading) {
			return;
		}
		if("".equals(password)){
			Toast.makeText(this,"请设置密码",Toast.LENGTH_SHORT).show();
			return;
		}
		RechargeParam atMeParam = new RechargeParam();
		atMeParam.setUserId(userPrefUtils.getUser().getId() + "");
		atMeParam.setPhone(userPrefUtils.getUser().getPhone() + "");
		atMeParam.setVerifyCode(modifyCode);
		atMeParam.setPwd(password);
		rechargeApi.setPass(atMeParam, this);
	}
	private void updatePassword() {
		if (isLoading) {
			return;
		}
		if("".equals(password)){
			Toast.makeText(this,"请设置密码",Toast.LENGTH_SHORT).show();
			return;
		}
		RechargeParam atMeParam = new RechargeParam();
		atMeParam.setUserId(userPrefUtils.getUser().getId() + "");
		atMeParam.setPhone(userPrefUtils.getUser().getPhone() + "");
		atMeParam.setVerifyCode(modifyCode);
		atMeParam.setPwd(oldpass);
		atMeParam.setNewPwd(password);
		rechargeApi.resetPass(atMeParam,this);
	}
	private void checkPassword() {
		if (isLoading) {
			return;
		}
		if("".equals(oldpass)){
			Toast.makeText(this,"请输入支付密码", Toast.LENGTH_SHORT).show();
			return;
		}
		RechargeParam atMeParam = new RechargeParam();
		atMeParam.setPwd(oldpass);
		rechargeApi.CheckPass(atMeParam,this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.main_back:
			finish();
			break;

		default:
			break;
		}
	}

	private void banDingDialog() {
		final DeleteDialog dialog = DeleteDialog.create(this, true);
		dialog.show();
		dialog.setCancelable(true);
		dialog.setTitle(StringUtils.getString(R.string.prompt));
		dialog.setContent("您输入的支付密码错误");
		dialog.setSubmittext("忘记密码");
		dialog.setCancle("重试");
		dialog.setCancelListener(new DeleteDialog.CancelClickListener() {

			@Override
			public void cancelClick() {
				dialog.dismiss();
				gridPasswordView.clearPassword();
			}
		});
		dialog.setSubmitListener(new DeleteDialog.SubmitClickListener() {

			@Override
			public void submitClick() {
				dialog.dismiss();
				Intent intent = new Intent(GzPayPassUpdateActivity.this,
						GzPayPassForgetActivity.class);
				intent.putExtra("frompage","forget");
				startActivity(intent);
				finish();

			}
		});
	}


	@Override
	public void onStart(String tag) {
		if (ApiConfig.APIURL.URL_CHECKPASS.equals(tag)){
			isLoading = true;
			if(isFirst){
				showDialog(StringUtils.getString(R.string.getting), true);
				isFirst = false;
			}
		}else if (ApiConfig.APIURL.URL_RestPAYPWD.equals(tag)){
			isLoading = true;
			if(isFirst){
				showDialog(StringUtils.getString(R.string.getting), true);
				isFirst = false;
			}
		}
	}

	@Override
	public void onSuccess(String response, String tag) {
		if (ApiConfig.APIURL.URL_CHECKPASS.equals(tag)) {
			isLoading = false;
			dismissDialog();
			if (JSONHelper.isSuccess(response)) {
				pass_tag.setText("请输入新的支付密码");
				password = "";
				gridPasswordView.clearPassword();
			} else if (JSONHelper.getResultCode(response) == 9121) {
				pass_tag.setText("请输入支付密码，验证身份");
				oldpass = "";
				gridPasswordView.clearPassword();
				banDingDialog();
			} else {
				pass_tag.setText("请输入支付密码，验证身份");
				oldpass = "";
				gridPasswordView.clearPassword();
				banDingDialog();
			}
		}else if (ApiConfig.APIURL.URL_SetPAYPWD.equals(tag) ||ApiConfig.APIURL.URL_RestPAYPWD.equals(tag)) {

			isLoading = false;
			dismissDialog();
			if (JSONHelper.isSuccess(response)) {
				SerializeManager.saveFile("true", Constant.peson_hpsw + userPrefUtils.getUser().getId());
//				if (!"forget".equals(frompage)) {
//					if ("update".equals(frompage)) {
//						Intent intent5 = new Intent(GzPayPassUpdateActivity.this, GzPayManageActivity.class);
//						startActivity(intent5);
//					} else {
//						Intent intent5 = new Intent(GzPayPassUpdateActivity.this, GzRedPaperCenterActivity.class);
//						startActivity(intent5);
//					}
//				}
				setResult(RESULT_OK);
				finish();
			} else if (JSONHelper.getResultCode(response) == 1003) {
				showToast(StringUtils.getString(R.string.verificationerror));
			} else {
				pass_tag.setText("请输入新的支付密码");
				password = "";
				gridPasswordView.clearPassword();
				Toast.makeText(GzPayPassUpdateActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
			}

		}
	}

	@Override
	public void onFaile(VolleyError volleyError, String tag) {
		if (ApiConfig.APIURL.URL_CHECKPASS.equals(tag)) {
			isLoading = false;
			dismissDialog();
			pass_tag.setText("请输入支付密码，验证身份");
			oldpass = "";
			gridPasswordView.clearPassword();
			ResultCode.toastVolleyError(activity, volleyError);
			Toast.makeText(GzPayPassUpdateActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
		}

		else if (ApiConfig.APIURL.URL_RestPAYPWD.equals(tag)) {
			isLoading = false;
			dismissDialog();
			pass_tag.setText("请输入新的支付密码");
			password = "";
			gridPasswordView.clearPassword();
			ResultCode.toastVolleyError(activity, volleyError);
			Toast.makeText(GzPayPassUpdateActivity.this, "设置失败", Toast.LENGTH_SHORT).show();

		}
		ResultCode.toastVolleyError(this, volleyError);
	}




}
